package com.weaving.monitor.service;

import com.weaving.monitor.dto.MachineDetailDto;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;

@Service
public class MachineDetailService {
    
    public MachineDetailDto getMachineDetail(Integer machineNumber) {
        MachineDetailDto detail = new MachineDetailDto();
        
        // 基本信息
        detail.setMachineNumber(machineNumber);
        detail.setCurrentSpeed(120);
        
        // 机器/订单详情
        detail.setMaterialType("坯布ATC-440X");
        detail.setOrderNumber("XXXXX-X");
        detail.setOrderQuantity(25000.0);
        detail.setCompletedQuantity(13527.0);
        detail.setCurrentMeters(251.3);
        
        // 工艺/疵点详情
        detail.setProcessCard("ATC-440X");
        detail.setProductionEmployee("周世奎");
        detail.setParkingDefectCount(5);
        detail.setNonParkingDefectCount(7);
        detail.setTotalDefectCount(12);
        
        // 梳栉/盘头详情
        detail.setGuideBarDetails(Arrays.asList(
            createGuideBarDetail("L1", "XX-XX-XX-XX"),
            createGuideBarDetail("L2", "XX-XX-XX-XX"),
            createGuideBarDetail("L3", "XX-XX-XX-XX"),
            createGuideBarDetail("L4", "XX-XX-XX-XX")
        ));
        
        // 图表数据
        detail.setChartData(createChartData());
        
        return detail;
    }
    
    private MachineDetailDto.GuideBarDetail createGuideBarDetail(String guideBarNumber, String machineBobbinNumber) {
        MachineDetailDto.GuideBarDetail detail = new MachineDetailDto.GuideBarDetail();
        detail.setGuideBarNumber(guideBarNumber);
        detail.setMachineBobbinNumber(machineBobbinNumber);
        return detail;
    }
    
    private MachineDetailDto.ChartData createChartData() {
        MachineDetailDto.ChartData chartData = new MachineDetailDto.ChartData();
        
        // 订单完成进度饼图数据
        chartData.setCompletionData(Arrays.asList(
            createCompletionData("已完成数量(米)", 13527.0, "#2196F3"),
            createCompletionData("剩余数量(米)", 11473.0, "#FF9800")
        ));
        
        // 故障分布散点图数据
        chartData.setFaultData(Arrays.asList(
            // 无故障
            createFaultData("无故障", 50.0, 0, "#2196F3"),
            createFaultData("无故障", 100.0, 0, "#2196F3"),
            createFaultData("无故障", 150.0, 0, "#2196F3"),
            createFaultData("无故障", 200.0, 0, "#2196F3"),
            createFaultData("无故障", 250.0, 0, "#2196F3"),
            // GB1
            createFaultData("GB1", 75.0, 1, "#87CEEB"),
            createFaultData("GB1", 125.0, 1, "#87CEEB"),
            createFaultData("GB1", 175.0, 1, "#87CEEB"),
            // 停车位
            createFaultData("停车位", 30.0, 2, "#000"),
            createFaultData("停车位", 80.0, 2, "#000"),
            createFaultData("停车位", 130.0, 2, "#000"),
            createFaultData("停车位", 180.0, 2, "#000"),
            createFaultData("停车位", 230.0, 2, "#000"),
            // 停车疵点
            createFaultData("停车疵点", 45.0, 3, "#F44336"),
            createFaultData("停车疵点", 95.0, 3, "#F44336"),
            createFaultData("停车疵点", 145.0, 3, "#F44336"),
            createFaultData("停车疵点", 195.0, 3, "#F44336"),
            createFaultData("停车疵点", 245.0, 3, "#F44336")
        ));
        
        // 时间图数据
        chartData.setTimeData(Arrays.asList(
            createTimeData("徐丽娜", 22, "#FF00FF"),
            createTimeData("周仕奎", 2, "#FFA500")
        ));
        
        return chartData;
    }
    
    private MachineDetailDto.CompletionData createCompletionData(String label, Double value, String color) {
        MachineDetailDto.CompletionData data = new MachineDetailDto.CompletionData();
        data.setLabel(label);
        data.setValue(value);
        data.setColor(color);
        return data;
    }
    
    private MachineDetailDto.FaultData createFaultData(String type, Double x, Integer y, String color) {
        MachineDetailDto.FaultData data = new MachineDetailDto.FaultData();
        data.setType(type);
        data.setX(x);
        data.setY(y);
        data.setColor(color);
        return data;
    }
    
    private MachineDetailDto.TimeData createTimeData(String employee, Integer hours, String color) {
        MachineDetailDto.TimeData data = new MachineDetailDto.TimeData();
        data.setEmployee(employee);
        data.setHours(hours);
        data.setColor(color);
        return data;
    }
} 